﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using EMSystem.EF;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;
using prince.CSSDemo.Models;
using System.Web.Script.Serialization;

namespace EMSystem.WebSite.Controllers
{
    public class BaseController : Controller
    {
        protected readonly string MenuJson = "~/Content/json/menu.json";
        protected EMSystemEntity dbContext;

        protected List<OrderMenu> _MenuList;

        public List<OrderMenu> MenuList
        {
            get
            {
                if (_MenuList == null)
                {
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    _MenuList = js.Deserialize<List<OrderMenu>>(GetJson(MenuJson)).ToList();
                }
                return _MenuList;
            }
            set
            {
                _MenuList = value;
            }
        }

        protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);
            if (dbContext == null)
                dbContext = new EMSystemEntity();
        }

        public virtual ContentResult Jsonp(object obj)
        {
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            Newtonsoft.Json.Formatting JsonFormat = Newtonsoft.Json.Formatting.Indented;
            string json = JsonConvert.SerializeObject(obj, JsonFormat, timeFormat);
            string jsonp = Request["jsonpcallback"];
            string result = jsonp + "(" + json + ")";
            return Content(result, "application/json", Encoding.UTF8);
        }

        public virtual ContentResult Jsonp(int code, string message, object data = null)
        {
            var obj = new { Code = code, Message = message, Data = data };
            return Jsonp(obj);
        }

        public virtual JsonResult Json(int code, string message, object data = null)
        {
            var obj = new { Code = code, Message = message, Data = data };
            return Json(obj);
        }



        protected string GetJson(string path)
        {
            try
            {
                string filepath = Server.MapPath(path);
                string json = GetFileJson(filepath);
                //IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
                //timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
                //Newtonsoft.Json.Formatting JsonFormat = Newtonsoft.Json.Formatting.Indented;
                //json = JsonConvert.SerializeObject(json, JsonFormat, timeFormat);
                //return Content(json, "application/json", Encoding.UTF8);
                return json;
            }
            catch (Exception ex)
            {
                //return Content(ex.Message);
                return string.Empty;
            }
        }
        //具体读取json文件数据
        protected string GetFileJson(string filepath)
        {
            string json = string.Empty;
            using (FileStream fs = new FileStream(filepath, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
            {
                using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312")))
                {
                    json = sr.ReadToEnd().ToString();
                }
            }
            return json;
        }

    }

    public static class Extension
    {
        public static T ToJsonObject<T>(this string str)
        {
            T obj = Activator.CreateInstance<T>();
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(str)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                return (T)serializer.ReadObject(ms);
            }
        }
    }

}